$(function () {
    window.page = 1
    var render = function () {
        getUsersData(function (data) {
            //渲染数据
            $('tbody').html(template('userData', data))
            //分页插件初始化
            $('.pagination').bootstrapPaginator({
                bootstrapMajorVersion: 3,
                size: 'small',
                currentPage: data.page,
                numberOfPages: 3,
                totalPages: Math.ceil(data.total/data.size),
                onPageClicked: function (event, originalEvent, type,page) {
                    window.page =  page
                    render()
                }
            })
        })
    }
    render()
    //禁用按钮的点击事件
    $('table').on('click','.yong', function () {
        var id = $(this).attr('data-id')
        var isDelete = $(this).hasClass('btn-danger')?0:1
        $.ajax({
            url: '/user/updateUser',
            type: 'post',
            data: {
                id: id,
                isDelete: isDelete
            },
            success: function (data) {
                if (data.success) {
                    render()
                }
            }
        })
    })
})
//查询所有用户信息
var getUsersData = function (callback) {
    $.ajax({
        url: '/user/queryUser',
        type: 'get',
        data: {
            page: window.page || 1,
            pageSize: 3
        },
        success: function(data) {
            callback && callback(data)
        }

    })
}